// pages/coupon/index.js
const service = require("../../../service/evaluate")
const rules = require("../../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 1,
    showtip: false,
    pageNum: 1, // 当前请求的page
    pageSize: 10, // 请求数据的size
    noData: false,
    commentGoodList:[],
    nomoredata: false
  },
  getList(type,callback) {
    if(type==1){
      service.getunComment({
        "data": {
          //用户id
          "customerId": wx.getStorageSync('userInfo').id
        },
        //分页
        "queryRequest": {
          //页码
          "pageNum": this.data.pageNum,
          //面数据量
          "pageSize": this.data.pageSize,
        }
  
      }, (res) => {
        console.log(res);
        
        callback(res);
  
      });
    }else{
      service.evaluateGoods({
        "data": {
          //用户id
          "customerId": wx.getStorageSync('userInfo').id
        },
        //分页
        "queryRequest": {
          //页码
          "pageNum": this.data.pageNum,
          //面数据量
          "pageSize": this.data.pageSize,
        }
  
      }, (res) => {
        console.log(res);
        
        callback(res);
  
      });
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  switchtag(e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index,
      pageNum:1
    })
    this.onLoad()
  },
  
  onLoad: function (options) {
    wx.showLoading({
      title: "加载中...",
    });
    this.getList(this.data.currentTab,res => {
      wx.hideLoading()
      if (res.code == "00000") {
        if (res.data.rows.length == 0) {
          this.setData({
            commentGoodList: res.data.rows,
            noData: true,
            nomoredata: false
          })
        } else {
          this.setData({
            commentGoodList: res.data.rows,
            noData: false,
            nomoredata: false
          })
        }


      }

    })
  },
  gocomment(e){
    wx.navigateTo({
      url: '/pages/evaluate/index?spuId='+e.currentTarget.dataset.item.spuId+"&orderDetAllId="+e.currentTarget.dataset.item.orderDetAllId,
    })
  },
  lookComment(e){
    wx.navigateTo({
      url: '/pages/evaluate/index?id='+e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNum: 1,
      noData: false,
    });
    const self = this;

    this.getList(this.data.currentTab,function (res) {
      console.log(res);
      if (res.code == "00000") {
        if (res.data.rows.length == 0) {
          self.setData({
            commentGoodList: res.data.rows,
            noData: true,
            nomoredata: false
          })
        } else {
          self.setData({
            commentGoodList: res.data.rows,
            noData: false,
            nomoredata: false
          })
        }

      }

      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.noData) return;
    wx.stopPullDownRefresh();
    const prolist = this.data.commentGoodList;
    let page = this.data.pageNum;
    this.setData({
      // 每次下拉 page+1
      pageNum: ++page,
    });
    const self = this;

    this.getList(this.data.currentTab,function (res) {
      if (res.data.rows.length == 0) {
        self.setData({
          noData: false,
          nomoredata: true
        });
      } else {
        res.data.rows.forEach((item) => {

          prolist.push(item);
        });
        self.setData({
          commentGoodList: prolist,
        });
      }
      // 隐藏加载状态
      // wx.hideNavigationBarLoading()
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})